﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using Economic.Api.Client;
using Economic.Api.Data;
using Economic.Api.Exceptions;
using Economic.Api;
using System.Configuration;

namespace TireService.Web
{
    public partial class EcoJobs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpdateCustomer_Click(object sender, EventArgs e)
        {
            // Instantiate data layer
            TireServiceDB db = new TireServiceDB();

            // Get customers
            List<Customer> customers = db.Customers.Where(p => p.CustomerId >= Convert.ToInt32(txtStart.Text) & p.CustomerId <= Convert.ToInt32(txtSlut.Text)).ToList();

            // Instantiate e-conomic API
            EconomicInterface ecoInterface = new EconomicInterface();

            // Create error string
            string errorMessage = "Job startet" + "\n";

            foreach (Customer customer in customers)
            {
                try
                {
                    ecoInterface.UpdateDebtor(customer, db);
                    System.Threading.Thread.Sleep(2000);
                    errorMessage += "Opdatering OK: Id " + customer.CustomerId + "\n";
                }
                catch (Exception ex)
                {
                    errorMessage += "Opdatering FEJL: Id " + customer.CustomerId + " Fejl besked: " + ex.Message + "\n";
                }
            }

            // Show job status
            txtJobStatus.Text = errorMessage;

            // Close database connection
            db.Dispose();
        }

        protected void btnUpdateSubscription_Click(object sender, EventArgs e)
        {
            // Instantiate data layer
            TireServiceDB db = new TireServiceDB();

            // Get customer subscriptions
            List<CustomerSubscription> subscriptions = db.CustomerSubscriptions.Where(p => p.CustomerId >= Convert.ToInt32(txtStart.Text) & p.CustomerId <= Convert.ToInt32(txtSlut.Text)).ToList();

            // Instantiate e-conomic API
            EconomicInterface ecoInterface = new EconomicInterface();

            // Create error string
            string errorMessage = "Job startet" + "\n";

            foreach (CustomerSubscription subscription in subscriptions)
            {
                try
                {
                    if (subscription.SubscriptionId != 4)
                    {
                        // Get customer
                        Customer customer = db.GetCustomer(subscription.CustomerId);

                        // Check if sync is required
                        if (customer.SyncToEconomic)
                        {
                            // Insert subscription in Economic
                            // Check if invoice is paid by parent customer
                            if (customer.ParentPayInvoice & customer.ParentCustomerId != null)
                            {
                                // Create subscription for parent customer
                                ecoInterface.UpdateSubscriber(subscription.SubscriptionId, subscription, Convert.ToInt32(customer.ParentCustomerId), customer.Name);
                            }
                            else
                            {
                                // Create subscription for customer
                                ecoInterface.UpdateSubscriber(subscription.SubscriptionId, subscription, null, customer.Name);
                            }
                            System.Threading.Thread.Sleep(2000);

                            errorMessage += "Opdatering OK: Id " + subscription.CustomerSubscriptionId + "\n";
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorMessage += "Opdatering FEJL: Id " + subscription.CustomerSubscriptionId + " Fejl besked: " + ex.Message + "\n";
                }
            }

            // Show job status
            txtJobStatus.Text = errorMessage;

            // Close database connection
            db.Dispose();
        }

    }
}